use  "../data/zip_year_data", clear

*Define CHTV and CHTC and the IV 
gen lnchtc = log((chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)))
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash))

gen lnchtv = log((chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval)))
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval))

by year, sort: egen chttc = sum(chtc)
gen chi_iv_ct = log(chineseper * chttc)
by year, sort: egen chttv = sum(chtv)
gen chi_iv_val = log(chineseper * chttv)

*Define the interacted terms
gen lnchtvXpost = lnchtv * post
gen lnchtcXpost = lnchtc * post
gen chi_iv_ctXpost = chi_iv_ct * post
gen chi_iv_valXpost = chi_iv_val * post

*Keep only common sample where both lnchtv and lnchtc are not missing
drop if lnchtc == .
drop if lnchtv == .

forvalue i= 2001/2013{
	gen lnchtv_`i' = lnchtv if year == `i'
	replace lnchtv_`i' = 0 if lnchtv_`i' ==. & lnchtv != .
	gen chi_iv_val_`i' = chi_iv_val if year == `i'
	replace chi_iv_val_`i' = 0 if chi_iv_val_`i'  == .
	}

drop lnchtv_2007 chi_iv_val_2007

*Zillow
ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 (lnchtv lnchtv_2* = chi_iv_val chi_iv_val_2*) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 

forvalue i = 2001/2006{
local beta_`i' = _b[lnchtv_`i']
local se_`i' = _se[lnchtv_`i']
}

forvalue i = 2008/2013{
local beta_`i' = _b[lnchtv_`i']
local se_`i' = _se[lnchtv_`i']
}

capture drop beta1 ul1 ll1 k ul2 ll2
gen beta1 = 0 in 7
gen ul1 = 0 in 7
gen ll1 = 0 in 7
gen ul2 = 0 in 7
gen ll2 = 0 in 7

forvalue j = 1/6{
local i = `j' + 2000
display `i' `j'
replace beta1 = `beta_`i'' in `j'
replace ul1 = `beta_`i'' + 1.96*`se_`i'' in `j'
replace ll1 = `beta_`i'' - 1.96*`se_`i'' in `j'
replace ul2 = `beta_`i'' + 1.645*`se_`i'' in `j'
replace ll2 = `beta_`i'' - 1.645*`se_`i'' in `j'
}	
 
forvalue j = 8/13{
local i = `j' + 2000
display `i' `j'
replace beta1 = `beta_`i'' in `j'
replace ul1 = `beta_`i'' + 1.96*`se_`i'' in `j'
replace ll1 = `beta_`i'' - 1.96*`se_`i'' in `j'
replace ul2 = `beta_`i'' + 1.645*`se_`i'' in `j'
replace ll2 = `beta_`i'' - 1.645*`se_`i'' in `j'
}	

gen k = 2001 in 1
replace k = k[_n-1] + 1 if k == .

preserve
keep in 1/13
twoway (line beta1 k, color(red)) (line ul1 k, lpattern(shortdash) color(red)) (line ll1 k, lpattern(shortdash) color(red)) (line ul2 k, lpattern(dot) color(red)) (line ll2 k, lpattern(dot) color(red)), ///
                ytitle(Log ZHVI) ///
                xtitle(Year) xlabel(2001(1)2013, labels angle(forty_five) valuelabel) ///
                graphregion(color(white)) bgcolor(white) ///
                ylabel(,nogrid) ///
                yline(0, lpattern(solid) lcolor(gs3)) ///
                legend(off)
graph export "../results/figureA6a.png", replace
restore


*Transaction value
ivreghdfe lntran_price lnpopulation d_lntran_price_9600 bachelorshare d_lnincome_9801 sa_nbr_bath age sa_sq sa_nbr_bed (lnchtv lnchtv_2* = chi_iv_val chi_iv_val_2*) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 

forvalue i = 2001/2006{
local beta_`i' = _b[lnchtv_`i']
local se_`i' = _se[lnchtv_`i']
}

forvalue i = 2008/2013{
local beta_`i' = _b[lnchtv_`i']
local se_`i' = _se[lnchtv_`i']
}

capture drop beta1 ul1 ll1 k ul2 ll2
gen beta1 = 0 in 7
gen ul1 = 0 in 7
gen ll1 = 0 in 7
gen ul2 = 0 in 7
gen ll2 = 0 in 7

forvalue j = 1/6{
local i = `j' + 2000
display `i' `j'
replace beta1 = `beta_`i'' in `j'
replace ul1 = `beta_`i'' + 1.96*`se_`i'' in `j'
replace ll1 = `beta_`i'' - 1.96*`se_`i'' in `j'
replace ul2 = `beta_`i'' + 1.645*`se_`i'' in `j'
replace ll2 = `beta_`i'' - 1.645*`se_`i'' in `j'
}	
 
forvalue j = 8/13{
local i = `j' + 2000
display `i' `j'
replace beta1 = `beta_`i'' in `j'
replace ul1 = `beta_`i'' + 1.96*`se_`i'' in `j'
replace ll1 = `beta_`i'' - 1.96*`se_`i'' in `j'
replace ul2 = `beta_`i'' + 1.645*`se_`i'' in `j'
replace ll2 = `beta_`i'' - 1.645*`se_`i'' in `j'
}	

gen k = 2001 in 1
replace k = k[_n-1] + 1 if k == .

preserve
keep in 1/13
twoway (line beta1 k, color(red)) (line ul1 k, lpattern(shortdash) color(red)) (line ll1 k, lpattern(shortdash) color(red)) (line ul2 k, lpattern(dot) color(red)) (line ll2 k, lpattern(dot) color(red)), ///
                ytitle(Log Transaction Value) ///
                xtitle(Year) xlabel(2001(1)2013, labels angle(forty_five) valuelabel) ///
                graphregion(color(white)) bgcolor(white) ///
                ylabel(,nogrid) ///
                yline(0, lpattern(solid) lcolor(gs3)) ///
                legend(off)
graph export "../results/figureA6c.png", replace
restore
